<?php
namespace App\Http\Controllers\Xcx;

use App\Service\Xcx\AddressService;
use Illuminate\Http\Request;

class AddressController extends BaseController
{
    //收货地址

    //列表
    public function index(Request $request)
    {
        $uid = $request->post('uid', 0);
        $res = AddressService::index($uid);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data'], $res['total']);
    }

    //详情
    public function one(Request $request)
    {
        $id = $request->post('id', 0);
        $res = AddressService::one($id);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    //获取默认
    public function defaultOne(Request $request)
    {
        $uid = $request->post('uid', 0);
        $res = AddressService::defaultOne($uid);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    //修改
    public function modify(Request $request)
    {
        $id = $request->post('id', 0);
        $uname = $request->post('uname', '');
        $pas['accepter'] = $request->post('accepter', '');
        $pas['phone'] = $request->post('phone', '');
        $pas['dizhi'] = $request->post('dizhi', '');
        if ($id) {
            $res = AddressService::modify($id, $uname, $pas);
        } else {
            $res = AddressService::add($uname, $pas);
        }
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //删除
    public function del(Request $request)
    {
        $id = $request->post('id', 0);
        $res = AddressService::del($id);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //默认
    public function todefault(Request $request)
    {
        $id = $request->post('id', 0);
        $uid = $request->post('uid', 0);
        $res = AddressService::todefault($id, $uid);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }
}